﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using ContractManager.Common.Class;
using ContractManager.Systems.Forms;
using DevExpress.LookAndFeel;
using DevExpress.Skins;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using Speed.Data;

namespace ContractManager
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            SkinManager.EnableFormSkins();
            BonusSkins.Register();
            LookAndFeelHelper.ForceDefaultLookAndFeelChanged();
            Application.SetCompatibleTextRenderingDefault(false);
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("vi-VN");
            string strError = "";
            //Cau hinh ket noi DB Speed
            Sys.ConnectionString = ClsVariable.StrSqlConnectionString;
            Sys.CommandTimeout = 1000;
            Sys.ProviderType = EnumDbProviderType.SqlServer;
            ClsVariable.Db = Sys.NewDb();
            //ClsVariable.EncryptConnectionString(true);
            //bool ret = ClsVariable.CheckLicense(ClsVariable.StrActivateCode, ref strError);
            //Application.Run(new FrmMain());
            bool ret = true;
            //ForceLoadAll(Assembly.GetExecutingAssembly());
            if (ret)
            {
                Application.Run(new FrmMain());
            }
            else
            {
                XtraMessageBox.Show(
                        "Không thể xác thực thông tin bản quyền phần mềm.",
                        "Thông báo",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                Application.Exit();
            }
        }

        public static void ForceLoadAll(Assembly assembly)
        {
            ForceLoadAll(assembly, new HashSet<Assembly>());
        }

        private static void ForceLoadAll(Assembly assembly,
                                 HashSet<Assembly> loadedAssmblies)
        {
            bool alreadyLoaded = !loadedAssmblies.Add(assembly);
            if (alreadyLoaded)
                return;

            AssemblyName[] refrencedAssemblies =
                assembly.GetReferencedAssemblies();

            foreach (AssemblyName curAssemblyName in refrencedAssemblies)
            {
                if (curAssemblyName.Name.Contains("Speed.") 
                    || curAssemblyName.Name.Contains("Npgsql")
                    || curAssemblyName.Name.Contains("MySql.Data")) continue;
                
                Assembly nextAssembly = Assembly.Load(curAssemblyName);
                if (nextAssembly.GlobalAssemblyCache)
                    continue;

                ForceLoadAll(nextAssembly, loadedAssmblies);
            }
        }
    }
}
